package com.sirdc.modules.sys.web.HardWork;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Project.entity.NewProjects;
import net.huashitong.Project.service.NewProjectsService;
import net.huashitong.node.entity.NodeNewschedules;
import net.huashitong.node.service.NodeNewschedulesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/dealWithNodeData")
public class DealWithNodeDataCtrl extends JsonBaseController {
    @Override
    protected String getView(String s) {
        return null;
    }

    @Autowired
    private NewProjectsService newProjectsService;
    @Autowired
    private NodeNewschedulesService nodeNewschedulesService;

    @ResponseBody
    @RequestMapping(value = "/nodeData", method = RequestMethod.GET)
    public Message nodeDataManage() {

        List<NewProjects> allProjects = newProjectsService.query();
        for (NewProjects p : allProjects) {
            List<NodeNewschedules> nodeList = nodeNewschedulesService.getNodeByProjectIdAndState(p.getSysId(), "");
            Collections.reverse(nodeList);
            String flag = "";
            //完成标识
            boolean flagBoo = true;
            for (NodeNewschedules m : nodeList) {
                //确定的节点
                if ("1".equals(m.getState())) {
                    //小节点
                    if ("2".equals(m.getType())) {
                        //已完成
                        if ("2".equals(m.getStatus())) {
                            flag = m.getNodeIds();
                            if (flagBoo) {
                                flagBoo = true;
                            }
                        } else {
                            flag = m.getNodeIds();
                            flagBoo = false;
                        }
                    }

                    //大节点
                    if ("1".equals(m.getType())) {
                        if (flag.equals(m.getNodeId())) {
                            if (flagBoo) {
                                //设置大节点完成
                                m.setStatus("2");
                            }
                            flagBoo = true;
                        }
                    }
                }
            }
            //批量保存
            nodeNewschedulesService.batchUpdate(nodeList);


            for (int i = 1; i < 37; i++) {
                String nodeId = i + "";
                //获取大节点
                NodeNewschedules bigNode = nodeNewschedulesService.getNnsByProjectIdAndNodeId(p.getSysId(), nodeId);
                if (ObjectUtils.isBlank(bigNode)) {
                    continue;
                }
                List<NodeNewschedules> nodes = new ArrayList<>();
                List<NodeNewschedules> nodesHaveFts = new ArrayList<>();
                for (NodeNewschedules node : nodeList) {
                    if (nodeId.equals(node.getNodeIds())) {
                        nodes.add(node);
                    }
                }
                //判断大节点下小节点是否全部完成
                //默认完成为：1
                boolean isFinished = true;
                for (NodeNewschedules n : nodes) {
                    if (!"2".equals(n.getStatus()) && "1".equals(n.getState())) {
                        isFinished = false;         //未完成
                        break;
                    } else {
                        if (StringUtils.isNotBlank(n.getFactFinishDate())) {
                            nodesHaveFts.add(n);
                        }
                    }
                }
                //全部完成进行时间排序
                if (isFinished && nodesHaveFts.size() > 0) {
                    Collections.sort(nodesHaveFts, new Comparator<NodeNewschedules>() {
                        public int compare(NodeNewschedules p1, NodeNewschedules p2) {
                            SimpleDateFormat format = new SimpleDateFormat("yyyy.mm.dd");
                            int result = 0;
                            if (p1.getFactFinishDate().equals(p2.getFactFinishDate())) {
                                return result;
                            }
                            try {
                                Date date1Str = format.parse(p1.getFactFinishDate());
                                Date date2Str = format.parse(p2.getFactFinishDate());
                                if (date1Str.after(date2Str)) {
                                    result = -1;
                                } else {
                                    result = 1;
                                }
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            return result;
                        }
                    });
                    String factFinishDate = nodesHaveFts.get(0).getFactFinishDate();
                    bigNode.setFactFinishDate(factFinishDate);
                    String planBeginDate = nodesHaveFts.get(nodesHaveFts.size() - 1).getPlanBeginDate();
                    bigNode.setPlanBeginDate(planBeginDate);
                    nodeNewschedulesService.update(bigNode);
                }
            }

        }
        return coverMessage("200", "修改成功");
    }
}
